/**
* @file label.cpp
*
* Copyright (c) Huawei Technologies Co., Ltd. 2025. All Rights reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "runtime/acl_rt_impl.h"

#include "runtime/rts/rts_model.h"
#include "log_inner.h"
#include "error_codes_inner.h"

aclError aclrtCreateLabelImpl(aclrtLabel *label)
{
    ACL_LOG_INFO("start to execute aclrtCreateLabel");
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(label);

    const rtError_t rtErr = rtsLabelCreate(static_cast<rtLabel_t*>(label));
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("call rtsLabelCreate failed, runtime result = %d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }

    ACL_LOG_INFO("successfully execute aclrtCreateLabel");
    return ACL_SUCCESS;
}

aclError aclrtSetLabelImpl(aclrtLabel label, aclrtStream stream)
{
    ACL_LOG_INFO("start to execute aclrtSetLabel");
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(label);
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(stream);

    const rtError_t rtErr = rtsLabelSet(static_cast<rtLabel_t>(label), static_cast<rtStream_t>(stream));
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("call rtsLabelSet failed, runtime result = %d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }

    ACL_LOG_INFO("successfully execute aclrtSetLabel");
    return ACL_SUCCESS;
}

aclError aclrtDestroyLabelImpl(aclrtLabel label)
{
    ACL_LOG_INFO("start to execute aclrtDestroyLabel");
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(label);

    const rtError_t rtErr = rtsLabelDestroy(static_cast<rtLabel_t>(label));
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("call rtsLabelDestroy failed, runtime result = %d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }

    ACL_LOG_INFO("successfully execute aclrtDestroyLabel");
    return ACL_SUCCESS;
}

aclError aclrtCreateLabelListImpl(aclrtLabel *labels, size_t num, aclrtLabelList *labelList)
{
    ACL_LOG_INFO("start to execute aclrtCreateLabelList, num is [%zu]", num);
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(labels);
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(labelList);

    const rtError_t rtErr = rtsLabelSwitchListCreate(static_cast<rtLabel_t*>(labels), num,
        reinterpret_cast<void**>(labelList));
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("call rtsLabelSwitchListCreate failed, runtime result = %d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }

    ACL_LOG_INFO("successfully execute aclrtCreateLabelList");
    return ACL_SUCCESS;
}

aclError aclrtDestroyLabelListImpl(aclrtLabelList labelList)
{
    ACL_LOG_INFO("start to execute aclrtDestroyLabelList");
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(labelList);

    const rtError_t rtErr = rtsLabelSwitchListDestroy(reinterpret_cast<void*>(labelList));
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("call rtsLabelSwitchListDestroy failed, runtime result = %d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }

    ACL_LOG_INFO("successfully execute aclrtDestroyLabelList");
    return ACL_SUCCESS;
}

aclError aclrtSwitchLabelByIndexImpl(void *ptr, uint32_t maxValue, aclrtLabelList labelList, aclrtStream stream)
{
    ACL_LOG_INFO("start to execute aclrtSwitchLabelByIndex, maxValue is [%u]", maxValue);
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(ptr);
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(labelList);
    ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(stream);

    const rtError_t rtErr = rtsLabelSwitchByIndex(ptr, maxValue, reinterpret_cast<void*>(labelList),
        static_cast<rtStream_t>(stream));
    if (rtErr != RT_ERROR_NONE) {
        ACL_LOG_CALL_ERROR("call rtsLabelSwitchByIndex failed, runtime result = %d", rtErr);
        return ACL_GET_ERRCODE_RTS(rtErr);
    }

    ACL_LOG_INFO("successfully execute aclrtSwitchLabelByIndex");
    return ACL_SUCCESS;
}
